<?php
/*
Write date to xml file use "example.php?w=1"
*/

require_once("../functions.php");
gadget_write();
html_header();

//Below gadget contents
?>

<script>

var MILLISECONDS_IN_DAY = 86400000.0;
var DAYS_IN_WEEK = 7.0;
var DAYS_IN_MONTH = 30.5;
var PREGNANCY_WEEKS = 40.0;
var MILLISECONDS_IN_WEEK = DAYS_IN_WEEK * MILLISECONDS_IN_DAY;
var MILLISECONDS_IN_MONTH = DAYS_IN_MONTH * MILLISECONDS_IN_DAY;

function $(e) {
	return document.getElementById(e);
}

function boby() {
	this.lmp = 0;
	this.duedate = 0;
	this.week = 0;
	this.weekday = 0;
	
	this.cookie = document.cookie;
	this.cookieName = "lmp";

	this.rmbFlag = true;
	this.rmbBtnName = "btnRmb";
}

boby.prototype.display = function(y, m, d, e) { 
	this.compute(y, m, d); 
	var cnweek = ["日", "一", "二", "三", "四", "五", "六"];
	var str = "预产期为"  + 
		this.duedate.getFullYear() + "年"  + 
		(this.duedate.getMonth()+1) + "月"  +  
		this.duedate.getDate() + "日（星期" + 
		cnweek[this.duedate.getDay()] + "）" ;
	
						
		if( this.week < 42 &&  this.week > -1) {
       str += "<br /><span title='从第0周的第1天算起'>今天是孕"  + 
			 this.week + 
			   "周的第"  + 
			 this.weekday + "天</span>";  
	} else {
		str += "<br />还没有开始";  
	}

	e.innerHTML = str;

	if(this.rmbFlag && navigator.cookieEnabled) {
		var cookieData = y + "-" + m + "-" + d;
		document.cookie = this.cookieName + "=" + encodeURIComponent(cookieData) +
                  		"; max-age=" + (60*60*24*365);
	}  else if(navigator.cookieEnabled) {
		document.cookie = this.cookieName + "=; max-age=" + (60*60*24*365);
	}


}

boby.prototype.compute = function(y, m, d) {

	y = parseInt(y);
	m = parseInt(m);
	d = parseInt(d);

	this.lmp = new Date(y, m - 1, d);
	this.lmp.setHours(0, 0, 0, 0);

	var diff = ((new Date()) - this.lmp);
	var weeks = Math.floor(diff / MILLISECONDS_IN_WEEK);
	//if (weeks < 0) weeks = 0;
	this.week = weeks;
	
	var days = Math.floor(diff / MILLISECONDS_IN_DAY) - weeks * DAYS_IN_WEEK;
	if (days < 0) days = 0;
	days += 1;
	this.weekday = days;
	
	var ms = (this.lmp.valueOf()) ;
	ms += MILLISECONDS_IN_DAY * 280 ;
	var duedate = new Date(ms);	
	
	this.duedate = duedate;
}

boby.prototype.rmb = function(e) {
	if (this.rmbFlag) {
		this.rmbFlag = false;
		e.checked = false;
	} else {
		this.rmbFlag = true;
		e.checked = true;
	}
}

boby.prototype.init = function() {
	var pos = this.cookie.indexOf(this.cookieName + "=");

	if (navigator.cookieEnabled == 1 && pos != -1) {
		var start = pos + this.cookieName.length + 1;
    	var end = this.cookie.indexOf(";", start);
    	if (end == -1) end = this.cookie.length;
    	var value = this.cookie.substring(start, end);
    	value = decodeURIComponent(value);
		value = value.split("-");
		y = value[0];
		m = value[1];
		d = value[2];
		this.display(y, m, d, $('msg'));
	} else {
		today = new Date( );
		y = today.getFullYear();
		m = today.getMonth()+1;
		d = today.getDate();
	}

	$('lmpy').value = y;
	$('lmpm').value = m;
	$('lmpd').value = d;
	
	this.rmbFlag = $('btnRmb').checked;

}

var bb = new boby();

</script>

<style>
<!--
.ct_input_text {
	border:1px solid #858585; 
}

body,p{font-family:arial}
a.bluelink:link{color:#0000cc;}
a.bluelink:hover{color:#0000cc;}
a.bluelink:visited{color:#800080;}
-->
</style>


<div>
<label title="末次月经" style="cursor:help" for="lmpy">LMP</label> 
<input type="text" size="4" id="lmpy" style="width:40px;" class="ct_input_text"/>年
<input type="text" size="2" id="lmpm" style="width:20px;" class="ct_input_text"/>月
<input type="text" size="2" id="lmpd" style="width:20px;" class="ct_input_text"/>日
&nbsp;&nbsp;
<button id="btncompute" onclick='bb.display($("lmpy").value, $("lmpm").value, $("lmpd").value, $("msg"))'>计算</button>

<br />
<label title="save" for="btnRmb">保存</label> 
<input type="checkbox" id="btnRmb" onclick='bb.rmb(this)' checked>

<br /><br />
<span id="msg">&nbsp;</span>
</div>

<script>
bb.init();
</script>
<?php
html_footer();
?>
